package com.kukudb.captcharepeater.serveice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kukudb.captcharepeater.pojo.Promoters;
import com.kukudb.captcharepeater.pojo.ResponseMessage;
import com.kukudb.captcharepeater.pojo.Users;
import com.kukudb.captcharepeater.repository.PromotersRepository;
import com.kukudb.captcharepeater.repository.UserRepository;
import com.kukudb.captcharepeater.serveice.interFaceTemp.UserServerInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class UserServerImpl implements UserServerInterface {
    @Autowired
    PromotersRepository promotersRepository;

    @Autowired
    UserRepository userRepository;
    @Autowired
    EmailServericeImpl emailServerice;

    @Override
    public ResponseMessage<String> getCode(Users user) {
        String phone = user.getPhone();

        log.info("phone:{}", phone);
        //查询推广员信息
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("promotion_code", user.getPromotionCode());
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper1);
//        String email = "";
        if (promoters1 == null) {
            log.info("推广员不存在");
            throw new RuntimeException("推广员不存在");
        }


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);

        Users users = userRepository.selectOne(queryWrapper);
        if (users == null) {
            log.info("不存在，新增用户");
            users = user;
//            users.setPhone(phone);
//            users.setPromotionCode(user.getPromotionCode());
            userRepository.insert(users);
            //将用户的数据封装成email
            //发送邮件
            emailServerice.sendPhoneEmailForAdmin(promoters1.getEmail(), phone);
            return new ResponseMessage<String>(200, "success", "验证码发送成功");

        } else if (users.getIsSuccess() == 1) {
            log.info("存在但是失败的订单重新发送验证码");
            users = user;
//            users.setPhone(phone);
            userRepository.update(users, queryWrapper);
            emailServerice.sendPhoneEmailForAdmin(promoters1.getEmail(), phone);
            return new ResponseMessage<String>(200, "success", "验证码发送成功");
        }
        //先判断数据库中有没有
        log.info("存在，不重复发送验证码");
        return ResponseMessage.error("该号码可能已经被注册，请勿重复发送验证码");
    }

    @Override
    public ResponseMessage<String> submitCode(Users user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", user.getPhone());
        Users users = userRepository.selectOne(queryWrapper);
        if (users != null) {
            users.setVerificationCode(user.getVerificationCode());
            users.setInvitationCode(user.getInvitationCode());
            users.setIsSuccess(2);
            users.setSubmitTime(new Date());
            int update = userRepository.update(users, queryWrapper);
            if (update == 1) {
                log.info("验证码提交成功");
                emailServerice.sendCodeForAdmin(user);
                return ResponseMessage.success("验证码提交成功");
            }
        }
        return ResponseMessage.error("验证码提交失败");
    }

    @Override
    public ResponseMessage<String> isOK(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        Users users = userRepository.selectOne(queryWrapper);
        if (users.getIsSuccess() == 0) {
            return ResponseMessage.success("已完成");
        } else if (users.getIsSuccess() == 1) {
            return ResponseMessage.error("失败");
        }
        return ResponseMessage.error("未处理");
    }
}
